﻿using System;
using System.Collections;
using System.Windows.Forms;

namespace DataUpload
{
    public partial class MappingCtrl : UserControl
    {
        private string _filePath;
        private ArrayList _requiredColumns;

        public string FilePath
        {
            get { return _filePath; }
        }

        public string IsValidMap
        {
            get { return CheckMap(); }
        }

        public MappingCtrl()
        {
            InitializeComponent();
        }

        public MappingCtrl(string filePath, ArrayList requiredColumns)
        {
            InitializeComponent();
            _filePath = filePath;
            _requiredColumns = requiredColumns;
        }

        private void lboxField_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (lboxField.SelectedItems.Count > 0)
            {
                dgvMapping.Rows.Add();
                dgvMapping.Rows[dgvMapping.Rows.Count - 1].Cells[0].Value = lboxField.SelectedItem.ToString();
                dgvMapping[0, dgvMapping.Rows.Count - 1].Selected = true;
            }
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            if (dgvMapping.Rows.Count > 0)
                dgvMapping.Rows.RemoveAt(dgvMapping.CurrentRow.Index);
        }

        private void btnMoveUp_Click(object sender, EventArgs e)
        {
            if (dgvMapping.Rows.Count < 2)
                return;

            int rowIndex = dgvMapping.CurrentRow.Index;
            DataGridViewRow currentRow = dgvMapping.CurrentRow;
            dgvMapping.Rows.RemoveAt(rowIndex);

            if (rowIndex - 1 >= 0)
                dgvMapping.Rows.Insert(rowIndex - 1, currentRow);
            else
                dgvMapping.Rows.Add(currentRow);

            dgvMapping.CurrentCell = dgvMapping[0, (rowIndex - 1 >= 0) ? rowIndex - 1 : dgvMapping.Rows.Count - 1];
            currentRow.Selected = true;
        }

        private void btnMoveDown_Click(object sender, EventArgs e)
        {
            if (dgvMapping.Rows.Count < 2)
                return;

            int rowIndex = dgvMapping.CurrentRow.Index;
            DataGridViewRow currentRow = dgvMapping.CurrentRow;
            dgvMapping.Rows.RemoveAt(rowIndex);

            if (rowIndex + 1 <= dgvMapping.Rows.Count)
                dgvMapping.Rows.Insert(rowIndex + 1, currentRow);
            else
                dgvMapping.Rows.Insert(0, currentRow);

            dgvMapping.CurrentCell = dgvMapping[0, (rowIndex + 1 < dgvMapping.Rows.Count) ? rowIndex + 1 : 0];
            currentRow.Selected = true;
        }

        public string CheckMap()
        {
            if (dgvMapping.Rows.Count == 0)
                return "No mapping has been done!";

            for (int rowIndex = 0; rowIndex < dgvMapping.Rows.Count; rowIndex++)
            {
                if (dgvMapping[1, rowIndex].Value == null || dgvMapping[0, rowIndex].Value == null)
                {
                    dgvMapping.Rows.RemoveAt(rowIndex);
                    rowIndex--;
                }
            }

            SortedList temp1 = new SortedList();
            SortedList temp2 = new SortedList();
            foreach (DataGridViewRow row in dgvMapping.Rows)
            {
                temp1.Add(row.Cells[1].Value, row.Cells[0].Value);
                temp2.Add(row.Cells[1].Value, row.Cells[2].Value);
            }

            dgvMapping.Rows.Clear();
            for (int i = 0; i < temp1.Count; i++)
            {
                object[] values = { temp1.GetByIndex(i), temp1.GetKey(i), temp2.GetByIndex(i) };
                dgvMapping.Rows.Add(values);
            }

            ArrayList temp = new ArrayList();
            string tbName = dgvMapping[1, 0].Value.ToString().Substring(0, dgvMapping[1, 0].Value.ToString().IndexOf("."));
            for (int i = 0; i < dgvMapping.Rows.Count; i++)
            {
                if (dgvMapping[1, i].Value.ToString().Substring(0, dgvMapping[1, i].Value.ToString().IndexOf(".")) == tbName)
                    temp.Add(dgvMapping[1, i].Value);
                else
                {
                    int j = 0;
                    while (j < _requiredColumns.Count)
                    {
                        if (_requiredColumns[j].ToString().Substring(0, _requiredColumns[j].ToString().IndexOf(".")) == tbName
                            && !temp.Contains(_requiredColumns[j]))
                            return "missing " + _requiredColumns[j].ToString();
                        j++;
                    }

                    tbName = dgvMapping[1, i].Value.ToString().Substring(0, dgvMapping[1, i].Value.ToString().IndexOf("."));
                    i--;
                    temp = new ArrayList();
                }
            }

            int k = 0;
            while (k < _requiredColumns.Count)
            {
                if (_requiredColumns[k].ToString().Substring(0, _requiredColumns[k].ToString().IndexOf(".")) == tbName
                    && !temp.Contains(_requiredColumns[k]))
                    return "missing " + _requiredColumns[k].ToString();
                k++;
            }

            return "ok";
        }
    }
}
